package com.dinghui.salary.utils;

import com.dinghui.salary.enums.ResponseStatusEnum;
import lombok.Getter;
import lombok.Setter;
import java.util.Map;


/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc 自定义响应数据结构
 */
@Setter
@Getter
public class ApiResult {

    // 响应业务状态码
    private Integer code;

    // 响应消息
    private String msg;

    // 是否成功
    private Boolean success;

    // 响应数据，可以是Object，也可以是List或Map等
    private Object data;

    public ApiResult() {}

    public ApiResult(Object data) {
        this.code = ResponseStatusEnum.SUCCESS.status();
        this.msg = ResponseStatusEnum.SUCCESS.msg();
        this.success = ResponseStatusEnum.SUCCESS.success();
        this.data = data;
    }

    public ApiResult(ResponseStatusEnum responseStatus) {
        this.code = responseStatus.status();
        this.msg = responseStatus.msg();
        this.success = responseStatus.success();
    }
    public ApiResult(ResponseStatusEnum responseStatus, Object data) {
        this.code = responseStatus.status();
        this.msg = responseStatus.msg();
        this.success = responseStatus.success();
        this.data = data;
    }
    public ApiResult(ResponseStatusEnum responseStatus, String msg) {
        this.code = responseStatus.status();
        this.msg = msg;
        this.success = responseStatus.success();
    }

    /**
     * 成功返回，带有数据的，直接往success方法丢data数据即可
     * @param data
     * @return
     */
    public static ApiResult success(Object data) {
        return new ApiResult(data);
    }

    /**
     * 成功返回，不带有数据的，直接调用success方法，data无须传入（其实就是null）
     * @return
     */
    public static ApiResult success() {
        return new ApiResult(ResponseStatusEnum.SUCCESS);
    }

    /**
     * 错误返回，直接调用error方法即可，当然也可以在ResponseStatusEnum中自定义错误后再返回也都可以
     * @return
     */
    public static ApiResult error() {
        return new ApiResult(ResponseStatusEnum.FAILED);
    }

    /**
     * 错误返回，map中包含了多条错误信息，可以用于表单验证，把错误统一的全部返回出去
     * @param map
     * @return
     */
    public static ApiResult errorMap(Map map) {
        return new ApiResult(ResponseStatusEnum.FAILED, map);
    }

    /**
     * 错误返回，直接返回错误的消息
     * @param msg
     * @return
     */
    public static ApiResult errorMsg(String msg) {
        return new ApiResult(ResponseStatusEnum.FAILED, msg);
    }

    /**
     * 错误返回，token异常，一些通用的可以在这里统一定义
     * @return
     */
    public static ApiResult errorTicket() {
        return new ApiResult(ResponseStatusEnum.TICKET_INVALID);
    }

    /**
     * 自定义错误范围，需要传入一个自定义的枚举，可以到[ResponseStatusEnum.java[中自定义后再传入
     * @param responseStatus
     * @return
     */
    public static ApiResult errorCustom(ResponseStatusEnum responseStatus) {
        return new ApiResult(responseStatus);
    }
    public static ApiResult exception(ResponseStatusEnum responseStatus) {
        return new ApiResult(responseStatus);
    }

}